package com.chinacoal.microservice.impl.controller;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.api.TaskExtAttrRegisterClient;
import com.chinacoal.microservice.api.model.entity.UtaskAttrEntity;
import com.chinacoal.microservice.impl.service.UtaskAttrService;
import com.chinacoal.microservice.login.LoginUser;
import com.chinacoal.microservice.model.IamUser;
import com.chinacoal.microservice.model.syscenter.ComboxEntity;
import com.chinacoal.microservice.util.result.CodeMsg;
import com.chinacoal.microservice.util.result.Result;

@RestController
public class UtaskAttrController implements TaskExtAttrRegisterClient {

    @Resource
    private UtaskAttrService utaskAttrService;
    @Resource
    private LoginUser loginUser;
    
	@Override
	public Result<Object> addRegAttr(UtaskAttrEntity utaskAttrEntity) {
    	try {
    		IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new Exception("用户未登录");
    		
			// 数据补全
			utaskAttrEntity.setCreationDate(new Date());
			utaskAttrEntity.setCreatedBy(iamUser.getUserId());
			utaskAttrEntity.setCreatedByName(iamUser.getName());
    		
			// 执行新增
			utaskAttrService.addRegAttr(utaskAttrEntity);
			
			return Result.success("新增待办类型扩展属性成功");   
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<Object> updateRegAttr(UtaskAttrEntity utaskAttrEntity) {
		try {
    		IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new Exception("用户未登录");
    		
			// 数据补全
			utaskAttrEntity.setLastUpdateDate(new Date());
			utaskAttrEntity.setLastUpdatedBy(iamUser.getUserId());
			utaskAttrEntity.setLastUpdatedByName(iamUser.getName());
    		
			// 执行更新
			utaskAttrService.updateRegAttr(utaskAttrEntity);
			
			return Result.success("更新待办类型扩展属性成功");   
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<Object> deleteRegAttr(String attrId) {
		try {
			// 执行删除
			utaskAttrService.deleteRegAttr(attrId);
			return Result.success("删除待办类型扩展属性成功");   
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<Page<UtaskAttrEntity>> getList(Integer page, Integer pageSize, String expression) {
		try {
    		Page<UtaskAttrEntity> utaskAttrPage = utaskAttrService.getList(page, pageSize, expression);
			return Result.success(utaskAttrPage);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<UtaskAttrEntity> getTaskAttr(String attrId) {
		try {
			UtaskAttrEntity utaskAttrEntity = utaskAttrService.selectById(attrId);
			return Result.success(utaskAttrEntity);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<List<ComboxEntity>> getTaskAttrComboBox() {
		try {
			List<ComboxEntity> utaskAttrCombox = utaskAttrService.getTaskAttrComboBox();
			return Result.success(utaskAttrCombox);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

    
}

